//
//  ZJMusicViewController.m
//  BreadTravel
//
//  Created by 张健 on 15/12/30.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ZJMusicViewController.h"
#import "ZJMusicModel.h"
#import "ZJMusicTableViewCell.h"
#import "ZJMusicDetailViewController.h"
@interface ZJMusicViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *array;

@end

@implementation ZJMusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"音乐电台";
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:0.191 green:0.226 blue:0.275 alpha:1.000];
    
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction)];
    
    self.myTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.rowHeight = 120;
    [self.view addSubview:self.myTableView];
    [self.myTableView registerClass:[ZJMusicTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.array = [NSMutableArray array];
    
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    hub.labelText = @"加载中";
    
    [AFNetworkTool getUrl:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/321797/true/1/30?device=iPhone" body:nil result:HXJSON headerFile:nil success:^(id result) {
        
        NSMutableDictionary *dic = result[@"tracks"];
        NSMutableArray *array = dic[@"list"];
        for (NSDictionary *dic in array) {
            
            ZJMusicModel *model = [[ZJMusicModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
        }
        
        [self.myTableView reloadData];
        
        [hub hide:YES];
        
    } failure:^(NSError *error) {
        
        
    }];
    
}

- (void)leftButtonAction{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"cell";
    ZJMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.model = [self.array objectAtIndex:indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZJMusicDetailViewController *detailVC = [ZJMusicDetailViewController shareDetailViewController];
    detailVC.trackId = [[self.array objectAtIndex:indexPath.row]trackId];
    detailVC.infoArray = self.array;
    detailVC.indexPath = indexPath.row;
    
    [self presentViewController:detailVC animated:YES completion:^{
        
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
